<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.merging_program_status" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="方案名称" prop="programName">
        <el-input v-model="queryParams.programName" placeholder="请输入方案名称" clearable/>
      </el-form-item>
      <el-form-item label="备注" prop="memo">
        <el-input v-model="queryParams.memo" placeholder="请输入备注" clearable/>
      </el-form-item>
      <el-form-item label="选择店铺：" prop="storeId">
        <el-select v-model="queryParams.storeId" placeholder="请选择店铺" filterable clearable>
          <el-option v-for="(item,index) in storeList" :key="index"
                     :label="item.lable"
                     :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productProgramList"
              @selection-change="handleSelectionChange" stripe border>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="方案名称" align="center" prop="programName"/>
      <el-table-column label="图片" align="center" prop="newImage">
        <template slot-scope="scope">
          <image-preview :src="scope.row.newImage" :width="80"></image-preview>
        </template>
      </el-table-column>
      <el-table-column label="店铺名称" align="center" prop="storeId">
        <template slot-scope="scope">
          {{ getStoreName(scope.row.storeId) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.merging_program_status" :value="scope.row.status"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="memo">
        <template slot-scope="scope">
          {{ scope.row.memo ? scope.row.memo : '暂无' }}
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="beginTime"/>
      <el-table-column label="结束时间" align="center" prop="endTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" @click="openAssociatedGoodsModal(scope.row)">关联在线商品</el-button>
          <el-button v-if="scope.row.status == '2'" size="mini" type="text"
                     @click="changeUseProgramme(scope.row,'0')">停用
          </el-button>
          <el-button v-else size="mini" type="text" @click="changeUseProgramme(scope.row,'1')">使用
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采集商品资质方案同步对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="方案名称" prop="programName">
          <el-input v-model="form.programName" placeholder="请输入方案名称"/>
        </el-form-item>
        <el-form-item label="店铺id" prop="storeId">
          <el-select v-model="form.storeId" :disabled="form.id" placeholder="请选择店铺" filterable>
            <el-option v-for="(item,index) in storeList" :key="index"
                       :label="item.lable" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品套图" prop="newImage">
          <image-upload v-model="form.newImage" :limit="1" :isShowTip="false"></image-upload>
        </el-form-item>
        <el-form-item label="开始时间" prop="beginTime">
          <el-date-picker clearable
                          v-model="form.beginTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
                          v-model="form.endTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="memo">
          <el-input v-model="form.memo" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--  店铺下在线商品列表  -->
    <storeOnlineProductList v-if="isOpenOnlineProductListModal" :programId="programId" :storeId="storeId"
                            @close="closeOnlineProductListModal"></storeOnlineProductList>

  </div>
</template>

<script>
import {
  addProductMergingProgram,
  editProductMergingProgram,
  sellerStoreProductProgramList, queryProductProgramDetail, sellerStoreProductMergingProgram
} from "@/api/product/online/productSet";
import {queryUserShopList} from "@/api/shop/index";
import storeOnlineProductList from "../components/StoreOnlineProductList.vue";

export default {
  name: "CollectProductQualificationProgramTask",
  dicts: ['merging_program_status'],
  components: {
    storeOnlineProductList
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采集商品资质方案同步表格数据
      productProgramList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        programId: undefined,
        collectProductId: undefined,
        sellerStoreProductId: undefined,
        aeProductId: undefined,
        storeId: undefined,
        status: undefined,
        programName: undefined,
        memo: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        programName: [{required: true, message: "方案名称不能为空", trigger: "blur"}],
        storeId: [{required: true, message: "请选择店铺", trigger: "change"}],
        memo: [{required: false, message: "备注不能为空", trigger: "blur"}],
        newImage: [{required: false, message: "请上传美工套图不能为空", trigger: "change"}],
        // timeRange: [{required: true, message: "请选择时间", trigger: "change"}],
        beginTime: [{required: true, message: "请选择开始时间", trigger: "change"}],
        endTime: [{required: true, message: "请选择结束时间", trigger: "change"}],
      },
      storeList: [],//店铺列表
      isOpenOnlineProductListModal: false,//是否打开在线商品列表弹窗
      programId: "",//方案id
      storeId: '',//店铺id
      // timeRange: [],//时间范围
    };
  },
  computed: {
    // 账户
    getStoreName() {
      return (val) => {
        var storeName = ''
        for (var i = 0; i < this.storeList.length; i++) {
          if (val == this.storeList[i].value) {
            storeName = this.storeList[i].lable
          }
        }
        return storeName
      }
    },
  },
  created() {
    this.getEmpowerList()
    this.getList();
  },
  methods: {
    // 获取店铺账号选择列表
    getEmpowerList() {
      this.loading = true;
      queryUserShopList().then(res => {
        this.storeList = res.data
        this.loading = false;
      });
    },
    /** 查询采集商品资质方案同步列表 */
    getList() {
      this.loading = true;
      sellerStoreProductProgramList(this.queryParams).then(response => {
        this.productProgramList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        programName: undefined,
        storeId: undefined,
        memo: undefined,
        beginTime: undefined,
        endTime: undefined,
        newImage: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品套图方案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      queryProductProgramDetail(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改商品套图方案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            editProductMergingProgram(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addProductMergingProgram(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    // 关联在线商品
    openAssociatedGoodsModal(row) {
      this.programId = row.id
      this.storeId = row.storeId
      this.isOpenOnlineProductListModal = true
    },
    // 关闭在线商品列表弹窗
    closeOnlineProductListModal(e) {
      // if (e == 'isRef') {
      this.getList()
      // }
      this.isOpenOnlineProductListModal = false
    },
    // 方案使用或停止
    changeUseProgramme(row, type) {
      let message = type == '1' ? '是否确定使用此方案？' : '是否确定停止使用此方案？'
      this.$confirm(message).then(() => {
        let data = {
          type: type,
          programId: row.id
        }
        sellerStoreProductMergingProgram(data).then(res => {
          this.$message.success(res.msg)
          this.getList()
        })
      })
    },
    /** 删除按钮操作 */
    // handleDelete(row) {
    //   const ids = row.id || this.ids;
    //   this.$modal.confirm('是否确认删除采集商品资质方案同步编号为"' + ids + '"的数据项？').then(() => {
    //     this.loading = true;
    //     return delCollectProductQualificationProgramTask(ids);
    //   }).then(() => {
    //     this.loading = false;
    //     this.getList();
    //     this.$modal.msgSuccess("删除成功");
    //   }).catch(() => {
    //   }).finally(() => {
    //     this.loading = false;
    //   });
    // },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gather/collectProductQualificationProgramTask/export', {
        ...this.queryParams
      }, `collectProductQualificationProgramTask_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
